package com.zys.service.impl;

import java.util.*;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import com.zys.dao.GoodDao;
import com.zys.dao.UserDao;
import com.zys.utils.DateUtils;
import com.zys.service.ReportService;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ReportServiceImpl implements ReportService {

  @Autowired
  private GoodDao goodDao;

  @Autowired
  private UserDao userDao;

  @Override
  public List<Integer> findGoodsCountByMonth(List<String> months) throws ParseException {
    List<Integer> goodsCount = new ArrayList<>();
    for (String month : months) {
      month = month + "-31 00:00:00";
      long time = DateFormat.getDateTimeInstance().parse(month).getTime();
      goodsCount.add(goodDao.findGoodCountBeforeDate(time));
    }
    return goodsCount;
  }

  @Override
  public Map<String, Object> getBusinessReport() {
    Map<String, Object> map = new HashMap<>();
    // 时间以毫秒为单位
    long today = DateUtils.getTodayZero().getTime();                    // 今天日期
    long todayEnd = DateUtils.getToday24().getTime();                   // 今天结束
    long thisMonday = DateUtils.getThisWeekMonday().getTime();          // 本周一
    long thisSunday = DateUtils.getThisWeekSunday().getTime();          // 本周日
    long firstDay = DateUtils.getFirstDay4ThisMonth().getTime();        // 本月一号
    long lastDay = DateUtils.getLastDay4ThisMonth().getTime() ;         // 本月最后一号

    // 创建一个 map 集合, 存储一段时间的起始值和结束值
    Map<String, Long> condition = new HashMap<>();
    // 今天
    condition.put("first", today);
    condition.put("last", todayEnd);
    map.put("todayNewUser", userDao.findUserCountByRegTime(condition));                     // 今天新增用户数量
    map.put("todayGoodNumber", goodDao.findUserCountByAddTime(condition));                  // 本日新增商品数

    // 本周
    condition.put("first", thisMonday);
    condition.put("last", thisSunday);
    map.put("thisWeekNewUser", userDao.findUserCountByRegTime(condition));                  // 本周新增用户数量
    map.put("thisWeekGoodNumber", goodDao.findUserCountByAddTime(condition));               // 本周新增商品数

    // 本月
    condition.put("first", firstDay);
    condition.put("last", lastDay);
    map.put("thisMonthNewUser", userDao.findUserCountByRegTime(condition));                 // 本月新增用户数量
    map.put("thisMonthGoodNumber", goodDao.findUserCountByAddTime(condition));              // 本周新增商品数

    map.put("reportDate", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));    // 今天日期
    map.put("totalUser", userDao.findUserTotalCount());                                     // 总的用户数
    map.put("totalGood", goodDao.findGoodTotalCount());                                     // 总的商品数

    return map;
  }
}
